import { Recording } from '../../../component/RecordingModal.tsx';
import { navigate } from '../../../common/navigationService.ts';
import Toast from 'react-native-toast-message';
import Tools from '../../../utils/Tools.ts';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Loading } from '../../../component/LoadingOverlay.tsx';
import to from '../../../utils/to.ts';
import HomeRequest from '../../../request/homeRequest.ts';
import { ShowPriceDiffCompProdModal } from '../../home/component/CompPriceDiffPopup.tsx';
import { ShowMergerCaseModal } from './component/MergerCaseModal.tsx';

interface IEventCollection {
  onPress: (data: IServerBtnListBtnList | IBtnFormatsList, moduleType?: number, btnList?: IServerBtnListBtnList[]) => void;
  onLongPress: (data: IServerBtnListBtnList | IBtnFormatsList) => void;
}

/**
 * 免支付下单与支付下单产品跳转前处理逻辑
 * @param data
 */
async function handleFreePayOrdAndPayOrdLogic(data: IServerBtnListBtnList | IBtnFormatsList) {
  const result = await AsyncStorage.getItem('persist:root');
  const beeperId: number = JSON.parse(result!).beeperId;
  Loading.show();
  const [err, res] = await to(HomeRequest.GetBtnDefaultGoods({ beeperId, btnId: data.id }));
  if (err) {
    Loading.hide();
    return;
  }
  const { goodsType } = res.data.goods;
  const { harvestAddressId } = res?.data?.addressInfo;
  if (!harvestAddressId) {
    Toast.show({ type: 'error', text1: '该产品没有地址信息' });
    return;
  }
  // 如果不是补差价产品
  if (goodsType && goodsType !== 15) {
    navigate('productSubmission', { data: res.data });
  } else {
    ShowPriceDiffCompProdModal.show({ data: res.data, eventId: data.eventId, linkUrl: data.linkUrl });
  }
  Loading.hide();
}

/**
 * 处理产品相关的界面判断
 * @param data
 */
const handleProductSwitch = async (data: IServerBtnListBtnList | IBtnFormatsList) => {
  const { eventId } = data;
  switch (eventId) {
    case 10:
      // 产品选购界面
      navigate('merchantProduct');
      break;
    case 11:
      // 弹窗支付下单 - 补差价产品
      await handleFreePayOrdAndPayOrdLogic(data);
      break;
    case 12:
      // 弹窗免付下单
      await handleFreePayOrdAndPayOrdLogic(data);
      break;
    case 13:
      console.log('详情页下单');
      break;
    case 14:
      console.log('结算页下单');
      break;
    case 18:
      console.log('挂号对接');
      break;
    case 19:
      console.log('影像预约对接');
      break;
  }
};

/**
 * 按钮、模版事件类型为点击事件
 * @param data
 * @param moduleType
 * @param btnList
 */
function handleClickOnEvent(data: IServerBtnListBtnList | IBtnFormatsList, moduleType?: number, btnList?: IServerBtnListBtnList[]) {
  const { eventId, linkUrl } = data;

  switch (eventId) {
    // 连接wifi'
    case 1:
      Toast.show({ type: 'error', text1: 'APP暂无此功能，请体验小程序' });
      break;
    // 拨打电话
    case 2:
      Tools.callPhone(linkUrl);
      break;
    // 合并按键
    case 6:
      if (btnList) ShowMergerCaseModal.show(btnList.filter((item) => Number(item.parentBtnCode) === data.btnCode));
      break;
    // 一键出院
    case 51:
      console.log('一键出院');
      break;
  }
}

/**
 * 按钮、模版事件为跳转内部界面
 * @param data
 * @param moduleType
 */
function handleJumpToInternalScreen(data: IServerBtnListBtnList | IBtnFormatsList, moduleType?: number) {
  const { eventId, linkUrl } = data;
  switch (eventId) {
    // 分类资讯界面
    case 3:
      console.log('分类资讯界面');
      break;
    // 聊天
    case 4:
      Toast.show({ type: 'error', text1: 'APP暂无此功能，请体验小程序' });
      break;
    // 活动报名
    case 5:
      console.log('活动报名');
      navigate('activityApply');
      break;
    // 物业通知
    case 7:
      console.log('物业通知');
      navigate('sendNotify');
      break;
    // 健康打卡
    case 53:
      console.log('健康打卡');
      navigate('healthClock');
      break;
    // 固定资讯
    case 54:
      console.log('固定资讯');
      break;
    // 设备详情
    case 55:
      console.log('设备详情');
      break;
    // 急救订单
    case 56:
      console.log('急救订单');
      break;
    // 通知详情
    case 57:
      console.log('通知详情');
      navigate('informDetails');
      break;
    // 受困上报
    case 58:
      console.log('受困上报');
      navigate('trappedReport');
      break;
    // 火情上报
    case 59:
      console.log('火情上报');
      break;
    // 物业缴费
    case 60:
      console.log('物业缴费');
      navigate('propertyPayment');
      break;
    // 消防报警
    case 61:
      console.log('消防报警');
      navigate('fireDetection');
      break;
  }
}

const EventCollection: IEventCollection = {
  // 触摸事件
  onPress(data, moduleType, btnList) {
    const { relationType, eventId, linkUrl, id, btnCode, serverTimeRange } = data;
    // 搜索跳转
    if (moduleType && moduleType === 6) {
      console.log('搜索跳转');
      return;
    }
    switch (relationType) {
      // 产品
      case 1:
        handleProductSwitch(data);
        break;
      //  web链接
      case 2:
        navigate('webViewScreen', { title: '', params: { uri: linkUrl } });
        break;
      // 小程序链接
      case 3:
        const _linkUrl = linkUrl.split(',');
        let params = null;
        if (_linkUrl.length > 1) {
          params = { path: _linkUrl[1], appId: _linkUrl[0] };
        } else {
          params = { shortLink: linkUrl };
        }
        console.log(params);
        break;
      // 点击事件
      case 4:
        handleClickOnEvent(data, moduleType, btnList);
        break;
      // 内部界面
      case 5:
        handleJumpToInternalScreen(data, moduleType);
        break;
    }
  },

  // 长按唤醒语音下单
  onLongPress(data) {
    const { relationType, eventId, voiceOrderFlag } = data;
    if (relationType === 1 && (eventId === 10 || eventId === 18 || eventId === 19) && voiceOrderFlag) {
      Recording.show();
    }
  },
};

export default EventCollection;
